package com.bjpowernode.threadpool.shutdown;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池 shutdownNow()方法
 *
 * @author Cat老师，关注我，抖音搜索：java512
 */
public class Test01 {

    public static void main(String[] args) throws Exception {
        //基于Executor框架实现线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                8,
                16,
                15,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(1024),
                Executors.defaultThreadFactory(),
                //Executors.privilegedThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );
        for (int i = 0; i < 2000; i++) {
            threadPoolExecutor.execute(() -> {
                System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + " 正在执行......");
            });
        }
        List<Runnable> taskList = threadPoolExecutor.shutdownNow();
        for (int i = 0; i < taskList.size(); i++) {
            System.out.println(taskList.get(i));
        }
        System.out.println("执行线程池关闭shutdownNow().");
    }
}